<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item :label="t('scxFinalJugde.fields.finalJudgeId')" prop="finalJudgeId">
        <el-input
          v-model="queryParams.finalJudgeId"
          :placeholder="t('common.inputTextPlaceholder') + t('scxFinalJugde.fields.finalJudgeId')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('scxFinalJugde.fields.finalSettingsId')" prop="finalSettingsId">
        <el-input
          v-model="queryParams.finalSettingsId"
          :placeholder="
            t('common.inputTextPlaceholder') + t('scxFinalJugde.fields.finalSettingsId')
          "
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item :label="t('scxFinalJugde.fields.confirmed')" prop="confirmed">
        <el-select
          v-model="queryParams.confirmed"
          :placeholder="t('common.selectTextPlaceholder') + t('scxFinalJugde.fields.confirmed')"
          clearable
          class="!w-240px"
          :disabled="advancedSearching"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item :label="t('scxFinalJugde.fields.judgeId')" prop="judgeId">
        <el-input
          v-model="queryParams.judgeId"
          :placeholder="t('common.inputTextPlaceholder') + t('scxFinalJugde.fields.judgeId')"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          :disabled="advancedSearching"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" :disabled="advancedSearching">
          <Icon icon="ep:search" class="mr-5px" />
          {{ t('common.search') }}
        </el-button>
        <el-button @click="resetQuery" :disabled="advancedSearching">
          <Icon icon="ep:refresh" class="mr-5px" />
          {{ t('common.reset') }}
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['awards:scx-final-jugde:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          {{ t('action.add') }}
        </el-button>
        <el-button
          type="warning"
          plain
          @click="handleImport"
          v-hasPermi="['awards:scx-final-jugde:export']"
        >
          <Icon icon="ep:upload" class="mr-5px" />
          {{ t('action.import') }}
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['awards:scx-final-jugde:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          {{ t('action.export') }}
        </el-button>
        <ConditionTree
          id="scxfinaljugde-condition-tree"
          :schemata="scxFinalJugdeSchema"
          @search="handleAdvanceSearch"
        />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <vxe-toolbar ref="toolbarRef" custom />
    <vxe-table
      id="iScxFinalJugde-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('scxFinalJugde.fields.id')"
        field="id"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalJugde.fields.finalJudgeId')"
        field="finalJudgeId"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalJugde.fields.finalSettingsId')"
        field="finalSettingsId"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalJugde.fields.confirmed')"
        field="confirmed"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('scxFinalJugde.fields.judgeId')"
        field="judgeId"
        align="center"
        show-header-overflow
        show-overflow
        min-width="100"
      />
      <vxe-column
        :title="t('common.operate')"
        field="operator"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['awards:scx-final-jugde:update']"
          >
            {{ t('action.edit') }}
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['awards:scx-final-jugde:delete']"
          >
            {{ t('action.delete') }}
          </el-button>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ScxFinalJugdeForm ref="formRef" @success="getList" />
  <ExcelImportForm
    ref="importFormRef"
    object-name="师昌绪终评-评委设置"
    upload-url="/awards/scx-final-jugde/import"
    template-url="/awards/scx-final-jugde/get-import-template"
    @success="getList"
  />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { ScxFinalJugdeApi, ScxFinalJugdeVO } from '@/api/awards/scxfinaljugde'
import ScxFinalJugdeForm from './ScxFinalJugdeForm.vue'
import ExcelImportForm from '@/components/ExcelImportForm/index.vue'
import type { VxeTableInstance, VxeTablePropTypes, VxeToolbarInstance } from 'vxe-table'
import { scxFinalJugdeSchema } from '@/views/awards/scxfinaljugde/schema'
import { useAppStore } from '@/store/modules/app'

/** 师昌绪终评-评委设置 列表 */
defineOptions({ name: 'ScxFinalJugde' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const toolbarRef = ref<VxeToolbarInstance>()
const tableRef = ref<VxeTableInstance>()

const loading = ref(true) // 列表的加载中
const list = ref<ScxFinalJugdeVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const advancedSearching = ref(false) // 高级搜索
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  advanced: undefined,
  finalJudgeId: undefined,
  finalSettingsId: undefined,
  confirmed: undefined,
  judgeId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ScxFinalJugdeApi.getScxFinalJugdePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  advancedSearching.value = false
  ;(queryParams as any).advanced = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref<InstanceType<typeof ScxFinalJugdeForm>>()
const openForm = (type: string, id?: number) => {
  formRef.value?.open(type, id)
}

/** 导入 */
const importFormRef = ref<InstanceType<typeof ExcelImportForm>>()
const handleImport = () => {
  importFormRef.value?.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ScxFinalJugdeApi.deleteScxFinalJugde(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ScxFinalJugdeApi.exportScxFinalJugde(queryParams)
    download.excel(data, '师昌绪终评-评委设置.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleAdvanceSearch = (base64Advanced?: string) => {
  ;(queryParams as any).advanced = base64Advanced
  advancedSearching.value = !!base64Advanced
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()

  const table = tableRef.value
  const toolbar = toolbarRef.value
  if (table && toolbar) {
    table.connect(toolbar)
  }
})
</script>
